package com.nativetalk.service.four.live;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.dict.TsNtDictIndex;
import com.nativetalk.bean.live.TdNtLiveMemberState;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtLiveCourseHisDao;
import com.nativetalk.dao.TdNtLiveMembDao;
import com.nativetalk.dao.TsNtDictDao;
import com.pingplusplus.Pingpp;
import com.pingplusplus.model.Charge;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzheng on 2017/2/27.
 */
@Service
public class FourLiveServiceImpl implements FourLiveService {
    private static Logger logger = Logger.getLogger(FourLiveServiceImpl.class);
    @Resource
    private TdNtLiveMembDao tdNtLiveMembDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TsNtDictDao tsNtDictDao;

    @Override
    public RetInfo memberLoginIn(TdNtMember tdNtMember, int live_cou_his_id) {
        String logInfo = this.getClass().getName() + ":memberLoginIn:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int memb_id = tdNtMember.getMemb_id();
            Map<String, Object> map = new HashMap<>();
            map.put("memb_id", memb_id);
            map.put("live_cou_his_id", live_cou_his_id);
            TdNtLiveMemberState tdNtLiveMemberState = tdNtLiveMembDao.findTdNtLiveMembByMembId(map);
            if (null == tdNtLiveMemberState) {
                tdNtLiveMembDao.insertTdNtLiveMemb(map);
            } else {
                map.put("live_memb_state", "1");//设置状态为进入直播
                tdNtLiveMembDao.updateTdNtLiveMemb(map);
            }

            /**
             * 直播课观看次数加一
             */
            tdNtLiveCourseHisDao.updateLiveCourseHisWatchSize(live_cou_his_id);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo memberLoginOut(TdNtMember tdNtMember, int live_cou_his_id) {
        String logInfo = this.getClass().getName() + ":memberLoginOut:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("memb_id", tdNtMember.getMemb_id());
        map.put("live_cou_his_id", live_cou_his_id);
        map.put("live_memb_state", "0");//设置状态为离开直播
        tdNtLiveMembDao.updateTdNtLiveMemb(map);
        retInfo.setMark(ValidationMessages.getValue("MARKYES"));
        retInfo.setTip("成功");

        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findMemberQueueInfo(int live_cou_his_id) {
        String logInfo = this.getClass().getName() + ":findMemberQueueInfo:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TdNtLiveMemberState> tdNtLiveMemberStates = tdNtLiveMembDao.findTdNtLiveMembQueueInfo(live_cou_his_id);
            for (int i = 0; i < tdNtLiveMemberStates.size(); i++) {
                tdNtLiveMemberStates.get(i).setIdentifier(Constants.member + tdNtLiveMemberStates.get(i).getMemb_id());
            }
            Map<String, Object> map = new HashMap<>();
            map.put("live_member_queue", tdNtLiveMemberStates);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setObj(map);
            retInfo.setTip("成功");

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveType(String app_type) {
        String logInfo = this.getClass().getName() + ":findLiveType:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String, Object> result = new HashMap<>();
        try {
            String code_type = "livetype";
            if(Constants.AppType.T.equalsIgnoreCase(app_type)){
                code_type="zhishishichangType";
            }
            List<TsNtDictIndex>  tsNtDictIndices = tsNtDictDao.findTsNtDictIndexs(code_type);
            result.put("live_type", tsNtDictIndices);
            retInfo.setObj(result);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取直播类别成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }

        return retInfo;
    }

    @Override
    public RetInfo liveTransfer(Integer memb_id, Float amount, String ip, String channel) {
        String logInfo = this.getClass().getName() + ":liveTransfer:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Pingpp.apiKey = Constants.Live_Secret_Key;
            Map<String, Object> chargeParams = new HashMap<String, Object>();
            chargeParams.put("order_no", CommonUtil.getOrderNub());
            chargeParams.put("amount", amount * 100);  //单位为分
//            chargeParams.put("amount", 1);  //单位为分，仅在测试的时候使用，否则请注释掉
            Map<String, String> app = new HashMap<String, String>();
            app.put("id", Constants.APP_ID);
            chargeParams.put("app", app);
            chargeParams.put("channel", channel);  //微信wx
            chargeParams.put("currency", "cny");
            chargeParams.put("client_ip", ip);
            chargeParams.put("subject", "外教君直播打赏");
            chargeParams.put("body", "外教君直播打赏");
            Charge charge = Charge.create(chargeParams);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("跳转成功");
            Map<String, Object> map = new HashMap<>();
            map.put("charge", charge);
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
